package com.dcivision.framework.taglib.channel;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.MessageResourcesFactory;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.workflow.bean.WorkflowProgress;
import com.dcivision.workflow.taglib.WorkflowDueDateTag;

public class AjaxWorkflowDudateTag extends WorkflowDueDateTag {

  private Log log = LogFactory.getLog(this.getClass());
  
  public String getAjaxContent() throws ApplicationException{
    try{
      this.init();
      return this.getContent();
    }catch(Exception ex){
      log.error(ex.getMessage(), ex);
      throw new ApplicationException(ex);
    }
  }
  
  protected String getContent() throws Exception {
   
    StringBuffer content = new StringBuffer();
    String dueDateLable = "";
    String color = "";
    String dueDateFormat = "";
    
    SessionContainer sessionContainer = (SessionContainer)this.pageContext.getSession().getAttribute(GlobalConstant.SESSION_CONTAINER_KEY);
    AjaxDurationTag durationTag = new AjaxDurationTag();
    durationTag.release();
    durationTag.setPageContext(this.pageContext);
    
    long seconds = 0;
    if (Utility.isEmpty(this.getDueDate())) {
      content.append("-");
    } else if (WorkflowProgress.STATUS_PENDING.equals(this.getStatus()) || WorkflowProgress.STATUS_IN_PROGRESS.equals(this.getStatus()) ) {
      durationTag.setStartDate(this.getDueDate().toString());
      durationTag.setEndDate(Utility.getCurrentTimestamp().toString());
      dueDateFormat = durationTag.getAjaxContent();
      seconds = (Utility.getCurrentTimestamp().getTime() - this.getDueDate().getTime()) / 1000;
      if (seconds > 0) {
        dueDateLable = "workflow.label.delay_sentence";
        color = "red";
        //dueDateFormat = new AjaxDataFormat(this.getDueDate(),Utility.getCurrentTimestamp(),"due").format();
      } else {
        dueDateLable = "workflow.label.due_date_remain";
        seconds = - seconds;
        color = "black";
        //dueDateFormat = new AjaxDataFormat(Utility.getCurrentTimestamp(),this.getDueDate(),"remain").format();
      }
      
      int days = new Float( (seconds) / 86400.0F ).intValue();
      int hours = new Float((seconds - (days * 86400.0F)) / (3600.0F)).intValue();
      int minutes = new Float((seconds - (days * 86400.0F) - (hours * 3600.0F)) / (60.0F)).intValue();
      
      content.append("<a href=\"javascript:void(0)\" style=\"text-decoration: none; cursor:text; \"  onmouseover=\"return getPopupInfo(\'" +
           (MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), dueDateLable, String.valueOf(days), String.valueOf(hours), String.valueOf(minutes) )) +
            "\');\" onmouseout=\"return nd();\">");
      content.append("<font color=\'" + color + "\'>" + dueDateFormat + "</font> </a>");
    
    } else {
      content.append("<font color=\'black\'>" + (TextUtility.getDateTimeFormat().format(this.getDueDate()))+ "</font> " );
    }
    return content.toString();
  }
}
